#include "helperView.h"
#include "model/curve_model.h"
#include "camera/ortho_camera.h"
#include "common_display_utils.h"

HelperView::HelperView(const Rectangle& viewport,SceneModel& model,Fog* fog)
	:	SceneView(viewport,model,fog){
	Cube viewVolume(Point(-50,-50,-60),Point(50,50,80));

	//Point hcenter = model.getTruck().getDimensions().center();
	Point hcenter = Point(0,0,0);
	camera = new OrthoCamera(hcenter+Point(0,0,3),hcenter,Point(0,1,0),viewVolume);

	id = glGenLists(1);
	glNewList(id, GL_COMPILE);
		CommonDisplayUtils::instance().drawXYGrid(model.getGround().cols(),model.getGround().tileWidth());
	glEndList();
}

HelperView::~HelperView(){

}

void HelperView::setEnv(){
	 glViewport ((GLint) viewport.x1,(GLint) viewport.y1, (GLsizei) viewport.width(), (GLsizei) viewport.height());
	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void HelperView::display(){
	SceneView::display();

	/*float m = model.getGround().cols()*model.getGround().tileWidth()*0.5f;
	glPushMatrix();
		glTranslatef(m,m,60.0f);
		glCallList(id);
	glPopMatrix();*/

	glDisable(GL_LIGHTING);
	glDisable(GL_TEXTURE_2D);
	glColor3f(1.0f,1.0f,1.0f);
	// render curve control points
	CurveModel::Points::const_iterator cit = spline.controlPointBegin();
	for(;cit!=spline.controlPointEnd();++cit){
		glPushMatrix();
		Point p = *cit;
		p.x3 += model.getGround().calculateZ(p.x1,p.x2);
		glTranslatef(p.x1,p.x2,p.x3);
			glutSolidCube(1.5f);
		glPopMatrix();
	}

	if(spline.controlSize()>3){
		spline.calculatePoints();
		CurveModel::Points::const_iterator it = spline.pointsBegin();
		glColor3f(1.0f,1.0f,0.0f);
		// render curve points
		for(;it!=spline.pointsEnd();++it){
			glPushMatrix();
				Point p = *it;
				p.x3 += model.getGround().calculateZ(p.x1,p.x2);
				glTranslatef(p.x1,p.x2,p.x3);
				glutSolidCube(1.3f);
			glPopMatrix();
		}
	}

	glEnable(GL_LIGHTING);
	glEnable(GL_TEXTURE_2D);
}
